<?php

namespace app\site\controller;

use app\common\model\ShopBundlingGoodsModel;
use app\common\model\ShopBundlingModel;
use think\Request;

class ShopBundlingController extends BaseController
{
    private $bundlingModel;
    private $bundlingGoodsModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->bundlingModel      = new ShopBundlingModel();
        $this->bundlingGoodsModel = new ShopBundlingGoodsModel();
    }

    /**
     * 活动列表
     */
    public function index()
    {
        $query          = $this->request->request();
        $map['site_id'] = $this->getSiteId();
        $status         = $this->request->param('status', -1);
        $blName         = $this->request->param('bl_name');
        if ($status >= 0) {
            $map['status'] = $status;
        }
        if (!empty($blName)) {
            $map['bl_name'] = ['like', '%' . $blName . '%'];
        }
        $list = $this->bundlingModel->where($map)->paginate(12, false, ['query' => $query]);
        $this->assign('query', $query);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加组合套装
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data            = $this->request->param();
            $data['site_id'] = $this->getSiteId();
            $result          = $this->validate($data, [
                'bl_name|活动名称'             => 'require',
                'sku_list|组合商品'            => 'require',
                'total_simple_price|原价格'   => 'float|>:0',
                'bl_discount_price|优惠套装价格' => 'float|>:0',
            ]);
            if (true !== $result) {
                $this->error($result);
            }
            $data['site_name'] = $this->getSiteInfo('site_name');
            if ($data['bl_freight_choose'] == 0) {
                $data['bl_freight'] = 0;
            }
            if ($data['bl_freight_choose'] == 1) {
                $data['bl_freight'] = (float)$data['bl_freight'];
            }
            if ($this->checkSkulist($data['sku_list']) === false) {
                $this->error('请正确填写，优惠价格必须小于销售价格！');
            }
            if ($this->bundlingModel->addData($data)) {
                $dataList = [];
                foreach ($data['sku_list'] as $value) {
                    $value['bl_id'] = $this->bundlingModel->bl_id;
                    $dataList[]     = $value;
                }
                $this->bundlingGoodsModel->saveAll($dataList);
                $this->success('添加成功！', url('index'));
            } else {
                $this->error($this->bundlingModel->getError());
            }
        }
        return $this->fetch();
    }

    /**
     * 修改活动
     */
    public function edit()
    {
        if($this->request->isAjax()){
            $data            = $this->request->param();
            $result          = $this->validate($data, [
                'bl_name|活动名称'             => 'require',
                'sku_list|组合商品'            => 'require',
                'total_simple_price|原价格'   => 'float|>:0',
                'bl_discount_price|优惠套装价格' => 'float|>:0',
            ]);
            if (true !== $result) {
                $this->error($result);
            }
            if ($data['bl_freight_choose'] == 0) {
                $data['bl_freight'] = 0;
            }
            if ($data['bl_freight_choose'] == 1) {
                $data['bl_freight'] = (float)$data['bl_freight'];
            }
            if ($this->checkSkulist($data['sku_list']) === false) {
                $this->error('请正确填写，优惠价格必须小于销售价格！');
            }
            if ($this->bundlingModel->editData($data)) {
                $dataList = [];
                foreach ($data['sku_list'] as $value) {
                    $dataList[]     = $value;
                }
                $this->bundlingGoodsModel->saveAll($dataList);
                $this->success('添加成功！', url('index'));
            } else {
                $this->error($this->bundlingModel->getError());
            }
        }
        $blId = $this->request->param('bl_id/d');
        $info = $this->bundlingModel->get($blId)->toArray();
        $item       = $this->bundlingGoodsModel->where('bl_id',$blId)->select()->toArray();
        $info['item'] =$item;
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除活动
     */
    public function del()
    {
        $blId = $this->request->param('bl_id/d');
        $this->bundlingGoodsModel->where('bl_id', $blId)->delete();
        $this->bundlingModel->where('bl_id', $blId)->delete();
        $this->success('操作成功！');
    }

    /**
     * 验证skulist
     * @param $skulist
     * @return bool
     */
    private function checkSkulist($skulist)
    {
        $valid = true;
        foreach ($skulist as $k => $v) {
            if (empty($v['bl_goods_price'])) {
                $valid = false;
                break; //终止循环
            }
            if ($v['bl_goods_price'] > $v['simple_price']) {
                $valid = false;
                break;
            }
        }
        return $valid;
    }
}